package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.depot.DepotOperator;
import com.scisoft.scan.data.depot.DepotOrderDetailBean;
import com.scisoft.scan.data.depot.DepotOrderStatis;
import com.scisoft.scan.data.depot.DepotQueryBean;
import com.scisoft.scan.data.pack.PackInfoBean;
import com.scisoft.scan.data.rent.RentOperator;
import com.scisoft.scan.data.rent.RentOrderBean;
import com.scisoft.scan.data.rent.RentOrderDetailBean;
import com.scisoft.scan.data.rent.RentOrderStatis;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_Rent {

    public static  String GetWS_Url(){
        return String.format("http://%s/dymps/ws/RentServer?wsdl", SysParameter.WS_IPandPort);
    }

    public  static  String WS_Method = "receive";

    public  static  String WS_Namespace = "http://rent.scan.ws.dms.com/";


    public static RentOrderStatis RENT_01() throws Exception {

//        RentOrderStatis ret = new RentOrderStatis();
//        ret.allOrderCount = 10;
//        ret.undoOrderCount = 1;
//        ret.doneOrderCount = 8;
//        ret.doingOrderCount = 1;
//        return ret;
        RentOrderStatis ret = new RentOrderStatis();
        String arg0 = String.format( "{\"flowNo\":\"RENT_01\",\"scSequence\":\"%s\",\"account\":\"%s\"}", SysParameter.ScSequence,SysParameter.user);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson rgson = new Gson();
            ret = rgson.fromJson(jsonArray.toString(),new TypeToken<RentOrderStatis>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;

    }

    //查询仓库订单(orderStatus 1全部2待拣货3拣货完成  )
    public static List<RentOrderBean> RENT_02(String orderStatus) throws Exception {
//        List<RentOrderBean> ret = new ArrayList<>();
//        RentOrderBean orderBean = new RentOrderBean();
//        orderBean.borrowId = "1111111111111111";
//        orderBean.orderTypeName = "活动订单";
//        orderBean.rentNo = "111111";
//        orderBean.orderNo = "22222";
//        orderBean.optime = "3333";
//        orderBean.orderInfo = "4444";
//        orderBean.orderState = "5555";
//        orderBean.operator = "张三";
//        orderBean.supplierName = "8888";
//        ret.add(orderBean);
//        return ret;
        List<RentOrderBean> ret = new ArrayList<>();
        String arg0 = String.format("{\"flowNo\":\"RENT_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user ,SysParameter.ScSequence, orderStatus);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(), new TypeToken<List<RentOrderBean>>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询一个仓库订单的详细信息（borrowId 订单编号）
    public static List<RentOrderDetailBean> RENT_03(String borrowId) throws Exception {

//        List<RentOrderDetailBean> ret = new ArrayList<>();
//        RentOrderDetailBean bean = new RentOrderDetailBean();
//        bean.dtlId = "1";
//        bean.partCode = "3";
//        bean.partNo = "2";
//        bean.partName = "3";
//        bean.supplierCode = "3";
//        bean.supplierName = "3";
//        bean.imgUrl = "3";
//        bean.position = "3";
//        bean.partCount = "3";
//        bean.notPickedCount = "3";
//        bean.pickedCount = "3";
//        bean.ifOk = "1";
//        bean.ifGyssm = "0";
//        bean.serialNo = "3";
//        ret.add(bean);
//        return ret;
        String arg0 = String.format( "{\"flowNo\":\"RENT_03\",\"bizId\":%s,\"scSequence\":\"%s\",\"account\":\"%s\"}",borrowId, SysParameter.ScSequence,SysParameter.user);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<RentOrderDetailBean> ret = new ArrayList<>();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson rgson = new Gson();
            ret= rgson.fromJson(jsonArray.toString(),new TypeToken<List<RentOrderDetailBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //获取当前订单的最后的操作员（orderId 订单编号）
    public static RentOperator RENT_04(String borrowId) throws Exception {
//        RentOperator ret = new RentOperator();
//        ret.operatorName = "张三";
//        return ret;
        String arg0 = String.format( "{\"flowNo\":\"RENT_04\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}",SysParameter.ScSequence,SysParameter.user,borrowId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        RentOperator ret = new RentOperator();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<RentOperator>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //强制更新当前订单的最后的操作员
    public static String RENT_05(String borrowId,RentOperator rentOperator) throws Exception {
//        return "成功";
        String bizId= borrowId+","+rentOperator.operatorName;
        String arg0 = String.format( "{\"flowNo\":\"RENT_05\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}",SysParameter.ScSequence,SysParameter.user,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析jso
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //单个明细 完成（dtlId 明细ID）
    public static String RENT_06(String dtlId)throws Exception {
//        return "成功";
        String arg0 = String.format( "{\"flowNo\":\"RENT_06\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,dtlId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {

        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //上传扫描配件的唯一码（orderId 订单编号 dtlId 明细ID,serialNo 唯一码）
    public static String RENT_07(String orderId,String dtlId,String serialNo)throws Exception {

//        return "成功";
        String bizId = dtlId+","+serialNo;
        String arg0 = String.format( "{\"flowNo\":\"RENT_07\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {

        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //删除扫描配件的唯一码（orderId 订单编号 dtlId 明细ID,serialNo 唯一码）
    public static String RENT_08(String orderId,String dtlId,String serialNo,String supplierCode)throws Exception {

        return "成功";
    }

    //单个明细处理取消 完成（dtlId 明细ID）
    public static String RENT_09(String dtlId)throws Exception {
        return "成功";
    }

    //所有完成 处理完成
    public static String RENT_10( String orderId)throws Exception {

        return "成功";
    }

    public static  SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope,headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
